import axios from "axios"
import queryString, { parse } from "qs"
import { Toast } from "antd-mobile";
/* 
开发环境：baseUrl="api"
生产环境：baseUrl=""
*/
var baseUrl = "/api"
if (process.env.NODE_ENV === "production") {
    baseUrl = "";
}

//请求拦截
axios.interceptors.request.use(config => {
    config.url = baseUrl + config.url;
    return config
})

// 响应拦截
axios.interceptors.response.use(res => {
    console.group("本次请求地址：" + res.config.url)
    console.log(res);
    console.groupEnd();
    //失败处理
    if (res.data.code !== 200) {
        Toast.show({
            icon: "fail",
            content: res.data.msg,
        })
    }
    return res;
})

// 封装get
export const get = (url, params = {}) => {
    return axios({
        url,
        method: "get",
        params
    })
}

//封装post
export const post = (url, params = {}, haveFile = false) => {
    let data = null;
    if (haveFile) {
        data = new FormData()
        for (let i in params) {
            data.append(i, params[i])
        }
    } else {
        data = queryString.stringify(params)
    }
    return axios({
        url,
        method: "post",
        data
    })
}